package com.wangsir.servlet;
import com.wangsir.entity.Clazz;
import com.wangsir.service.ClazzService;
import com.wangsir.util.ApiResult;
import com.wangsir.util.MyUtil;
import com.wangsir.util.PagerVO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/clazz")
public class ClazzServlet extends HttpServlet {
    private ClazzService clazzService = new ClazzService();
    //处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String r = req.getParameter("r");
        //只要不是修改和新增都是班级列表页面
        if(r == null) {
            String currentPage = req.getParameter("currentPage");
            //第一次请求这个方法的时候是不加参数的，给current一个默认的值，默认第一次访问是第一页
            if (currentPage == null) {
                currentPage = "1";
            }
            String clazzno = req.getParameter("clazzno");
            String name = req.getParameter("name");
            PagerVO<Clazz> pageVo = clazzService.page(Integer.parseInt(currentPage), 10, clazzno, name);
            pageVo.init();
            List<Clazz> list = pageVo.getList();
            for (Clazz clazz : list) {
                System.out.println(clazz);
            }
            req.setAttribute("clazzno", clazzno);
            req.setAttribute("name", name);
            req.setAttribute("pageVo", pageVo);
            req.getRequestDispatcher("/WEB-INF/view/clazz-list.jsp").forward(req, resp);
        }
        if("add".equals(r)){
            boolean isPermission = MyUtil.hasPermission(req,resp,false,"admin");
            //没有权限就不进行跳转，直接return
            if(!isPermission){
                return;
            }
            req.getRequestDispatcher("/WEB-INF/view/clazz-add.jsp").forward(req, resp);
        }
        if("edit".equals(r)){
            boolean isPermission = MyUtil.hasPermission(req,resp,false,"admin");
            //没有权限就不进行跳转，直接return
            if(!isPermission){
                return;
            }
            String clazzno = req.getParameter("clazzno");
            System.out.println("clazzno:" + clazzno);
            Clazz clazz1 = clazzService.getByClazznNO(clazzno);
            req.setAttribute("entity",clazz1);
            System.out.println(clazz1);
            req.getRequestDispatcher("/WEB-INF/view/clazz-edit.jsp").forward(req, resp);
        }
    }



    //处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //新增 修改 删除
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        //
        String r = req.getParameter("r");
        if("add".equals(r) || "edit".equals(r)) {
            boolean isPermission = MyUtil.hasPermission(req,resp,true,"admin");
            //没有权限就不进行跳转，直接return
            if(!isPermission){
                return;
            }
            //班级的主键
            String clazzno = req.getParameter("clazzno");
            //班级的名字
            String name = req.getParameter("name");
            Clazz clazz = new Clazz();
            clazz.setClazzNo(clazzno);
            clazz.setName(name);
            if("add".equals(r)){
                //add
                String msg = clazzService.insertClazz(clazz);
                if(msg != null){
                    resp.getWriter().write(ApiResult.json(false,msg));
                    return;
                }else{
                    resp.getWriter().write(ApiResult.json(true,"保存班级成功！"));
                    return;
                }
            }else{
                //edit
                String msg = clazzService.updateClazz(clazz);
                if(msg != null){
                resp.getWriter().write(ApiResult.json(false,msg));
                return;
                }else{
                resp.getWriter().write(ApiResult.json(true,"更新班级成功！"));
                return;
                }
            }
        }else{
            boolean isPermission = MyUtil.hasPermission(req,resp,true,"admin");
            //没有权限就不进行跳转，直接return
            if(!isPermission){
                return;
            }
            //del
            //下面的这个变量名必须要和前端传过来的data的属性名称一样，否则属性名称是无法进行解析的
            String clazzNo = req.getParameter("clazzNo");
            System.out.println(clazzNo);
            String msg = clazzService.delete(clazzNo);
            System.out.println(msg);
            if(msg != null){
                resp.getWriter().write(ApiResult.json(false,msg));
                return;
            }else{
                resp.getWriter().write(ApiResult.json(true,"删除班级成功！"));
                return;
            }

        }
    }
}
